/*
 * Copyright 2007-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.examples.controller;

import net.ymate.framework.core.util.WebUtils;
import net.ymate.platform.core.i18n.I18N;
import net.ymate.platform.webmvc.annotation.Controller;
import net.ymate.platform.webmvc.annotation.RequestHeader;
import net.ymate.platform.webmvc.annotation.RequestMapping;
import net.ymate.platform.webmvc.context.WebContext;
import net.ymate.platform.webmvc.view.IView;
import net.ymate.platform.webmvc.view.View;
import org.apache.commons.lang.StringUtils;

import java.util.Locale;

/**
 * 语言切换控制器
 *
 * @author 刘镇 (suninformation@163.com) on 16/3/15 下午10:52
 * @version 1.0
 */
@Controller
@RequestMapping("/language")
public class LanguageController {

    /**
     * 切换为中文
     *
     * @param refererUrl 来源连接
     * @return 返回来源连接地址
     */
    @RequestMapping("/zh_CN")
    public IView __zh_CN(@RequestHeader("Referer") String refererUrl) {
        I18N.change(Locale.SIMPLIFIED_CHINESE);
        if (StringUtils.isBlank(refererUrl)) {
            refererUrl = WebUtils.buildURL(WebContext.getRequest(), "dashboard", true);
        }
        return View.redirectView(refererUrl);
    }

    /**
     * 切换为英文
     *
     * @param refererUrl 来源连接
     * @return 返回来源连接地址
     */
    @RequestMapping("/en_US")
    public IView __en_US(@RequestHeader("Referer") String refererUrl) {
        I18N.change(Locale.US);
        if (StringUtils.isBlank(refererUrl)) {
            refererUrl = WebUtils.buildURL(WebContext.getRequest(), "dashboard", true);
        }
        return View.redirectView(refererUrl);
    }
}
